# Your game logic all goes here
from pyston.pyston import *
from random import *

# Main entrypoint
# All your code goes here
class Game:
    
    def main(self):
        # Create an instance of pyston and initialise it
        self.engine = Pyston()
        
        # Checking for quitting
        self.quit = False
        
        # Make some shortcuts
        self.rm = self.engine.resourceManager
        self.em = self.engine.entityManager
        
        # Test resource manager
        playerImage = self.rm.loadResource("player", "player.bmp")
        
        
        self.dudes = [DynamicEntity(self.engine.background), DynamicEntity(self.engine.background)]
        
        # Test dynamic entity
        self.dudes[0].setPosition(10, 10)
        self.dudes[0].setUpdateInterval(1)
        self.dudes[0].setImage(playerImage)
        self.em.addEntity("dude_1", self.dudes[0])
        
        self.dudes[1].setPosition(10, 10)
        self.dudes[1].setUpdateInterval(1)
        self.dudes[1].setImage(playerImage)
        self.em.addEntity("dude_2", self.dudes[1])
        
        # Test logical entity
        self.testLogic = LogicalEntity()
        self.testLogic.setUpdateInterval(1)
        self.em.addEntity("Trigger", self.testLogic)
        
        self.tick = 0
        self.hasRemoved = False
        
        self.twoX = 0
        self.twoY = 0
        
        x = 0
        y = 0
        
        while 1:
            if self.quit == True:
                break
            
            # Do this first
            self.engine.gatherEvents()
            
            # Our own input processing
            self.processInput()
            
            # Test
            self.dudes[0].x = x
            self.dudes[0].y = y
            self.dudes[1].setPosition(self.twoX, self.twoY)
            x += 1
            y += 1

            # Finally step the engine
            self.engine.stepFrame()

        # Exit
        sys.exit()
    
    def processInput(self):
        for event in self.engine.events:
            if event.type == pygame.QUIT:
                self.quit = True
            elif event.type == pygame.KEYDOWN:
                if event.key == K_RIGHT:
                    self.twoX += 3
                if event.key == K_LEFT:
                    self.twoX -= 3
                if event.key == K_UP:
                    self.twoY -= 3
                if event.key == K_DOWN:
                    self.twoY += 3
                if event.key == K_DELETE:
                    self.rm.unloadImage("player")
                    
                if event.key == K_ESCAPE:
                    self.quit = True


if __name__ == "__main__":
    MainGame = Game()
    MainGame.main()